"""
@date:2023/3/30
@file:三乐购物系统.py
@author:lzx
@desc:test
"""

def login(username, passwd):
    if username in user and user[username]['passwd'] == passwd:
        return True

def register(username, passwd, money):
    if username in user:
        print("用户名已存在，请重新输入！")
    else:
        user[username] = {'passwd':passwd, 'money':money}
        print(f"{username}注册成功")
        print("用户信息已更新".center(20,"*"))
        print(f"{'用户名':<8}{'密码':<10}{'金额':<7}")
        for i in user:
            print(f"{i:<10}{user[i]['passwd']:<12}{user[i]['money']:<10}")


def message():
    print("商品信息".center(40,"*"))
    print(f"{'商品编号':<7}{'商品名称':<8}{'单价':<7}")
    for i in goodsmess:
        print(f"{i:<10}{goodsmess[i]['name']:<10}{goodsmess[i]['price']:<10}")
    print("*"*40)


def add_goods(goodsid, goodsnum):
    cart[goodsid] = cart.get(goodsid, 0) + goodsnum
    print("加购成功...")
    print(f"{'商品编号':<8}{'数量':<7}")
    print(f"{goodsid:<12}{goodsnum:<10}")

def check_cart():
    print("查看购物车".center(40,"*"))
    print(f"{'商品编号':<8}{'商品名称':<8}{'单价':<7}{'数量':<7}")
    for i in cart:
        print(f"{i:<11}{goodsmess[i]['name']:<10}{goodsmess[i]['price']:<8}{cart[i]:<10}")
    print("*"*40)

def pay_cart(pay):
    global cartmoney, cartmoney2
    for i in cart:
        cartmoney += cart[i] * goodsmess[i]['price']
    if cartmoney <= user[username]['money'] :
        user[username]['money'] -= cartmoney
        print(f"结账成功，本次消费：{cartmoney}，余额：{user[username]['money']}")
        cart2.update(cart.copy())
        cart.clear()
        cartmoney2 += cartmoney
        cartmoney = 0
    else:
        print(f"余额不足！")


def exit():
    global cartmoney2
    print("谢谢惠顾！欢迎下次光临！".center(30,"*"))
    print("*"*40)
    print(f"{'商品编号':<8}{'商品名称':<8}{'单价':<7}{'数量':<7}")
    for i in cart2:
        print(f"{i:<11}{goodsmess[i]['name']:<10}{goodsmess[i]['price']:<8}{cart2[i]:<10}")
    print("*" * 40)
    print(f"总共消费：{cartmoney2}，余额为：{user[username]['money']}")
    cart2.clear()
    cartmoney2 = 0

#保存用户信息
user = {'root':{'passwd':'123456', 'money':1000}}

print("欢迎进入三乐购物系统".center(50,"*"))
#保存商品信息
goodsmess = {'F00001':{'name':'毛衣','price':100},
             'F00002':{'name':'夹克','price':200},
             'F00003':{'name':'羽绒服','price':800},
             'F00004':{'name':'鞋子','price':500},
             'F00005':{'name':'墨镜','price':80},
             'F00006':{'name':'牛仔裤','price':120}}

cart = {}
cart2 = {}
cartmoney = 0
cartmoney2 = 0
while 1:
    print("1、登录".center(50))
    print("2、注册".center(50))
    print("3、退出".center(50))
    option = input("请输入你的选择：")
    if option == '1':
        username = input("请输入用户名：")
        passwd = input("请输入密码：")
        if login(username,passwd):
            print("登录成功")
            print(f"欢迎光临！你的余额为：{user[username]['money']}")
            while 2:
                print("1、查看商品信息".center(50))
                print("2、购买商品，将商品加入购物车".center(58))
                print("3、查看和结算购物车".center(52))
                print("4、退出".center(46))
                option2 = input("请输入你的选择：")
                if option2 == '1':
                    message()
                elif option2 == '2':
                    goodsid = input("请输入你想购买的商品编号(F00001~F00006)：")
                    if goodsid in goodsmess:
                        goodsnum = input("请输入你想购买的商品数量：")
                        if goodsnum.isdigit():
                            goodsnum = int(goodsnum)
                            add_goods(goodsid, goodsnum)
                        else:
                            print("输入的不是整数")
                    else:
                        print("商品不存在！")
                elif option2 == '3':
                    check_cart()
                    pay = input("结算购物车请按1(按其他键返回菜单)：")
                    if pay == '1':
                        pay_cart(pay)
                elif option2 == '4':
                    print("退出")
                    exit()
                    break
                else:
                    print("输入错误！")
        else:
            print("登录失败")
    elif option == '2':
        username = input("请输入用户名：")
        passwd = input("请输入密码：")
        money = int(input("请输入金额："))
        register(username,passwd,money)
    elif option == '3':
        print("退出")
        break
    else:
        print("输入错误！")
